package org.luxor.commons.playwright.listener;

import com.microsoft.playwright.Page;
import org.luxor.commons.core.utils.UuidUtils;
import org.luxor.commons.playwright.component.Context;
import org.luxor.commons.playwright.component.command.PlaywrightCommand;

/**
 * Selenium 事件监听器接口
 *
 * @author Mr.yan @date 2022/10/24
 */
public interface PlaywrightEventListener {

    /**
     * 初始化执行指令
     *
     * @param command 业务指令
     * @param context 业务上下文
     */
    default void initExecute(PlaywrightCommand command, Context context) {
        context.setTrackId(UuidUtils.uuid32());
        context.setSessionId(UuidUtils.uuid());
    }


    /**
     * 指令执行之前
     *
     * @param page    当前页
     * @param command 业务指令
     * @param context 业务上下文
     */
    void beforeExecute(Page page, PlaywrightCommand command, Context context) throws Exception;

    /**
     * 指令执行之后
     *
     * @param page    当前页
     * @param command 业务指令
     * @param context 业务上下文
     */
    void afterExecute(Page page, PlaywrightCommand command, Context context) throws Exception;

    /**
     * 异常处理
     *
     * @param throwable 异常
     * @param page      当前页
     */
    void onException(Throwable throwable, Page page);
}
